<template>
    <div>
        <el-row>
            <el-col :span="6">
                动态加载：
                <el-cascader
                    filterable
                    clearable
                    :props="props"
                    @change="handleChange">
                </el-cascader>
            </el-col>
            <el-col :span="6">
                单选可搜索：
                <el-cascader
                    placeholder="试试搜索:指南"
                    :options="this.jsonData"
                    filterable></el-cascader>
            </el-col>
            <el-col :span="6">
                自定节点内容：
                <el-cascader :options="this.jsonData">
                    <template slot-scope="{node,data}">
                        <span>{{data.label}}</span>
                        <span v-if="!node.isLeaf" style="color: brown">({{data.children.length}})</span>
                    </template>
                </el-cascader>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="24">
                级联面板：
                <el-cascader-panel :options="this.jsonData" @change="handleChange"></el-cascader-panel>
            </el-col>
        </el-row>
    </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'

@Component({})
export default class CascadeComponent extends Vue {
    name = '';
    jsonData = [];
    props = {
        lazy:true,
        value:"id",
        label:"orgName",
        lazyLoad (node:any, callback:any):void {
            let level = node.level;
            if (level == 0) {
                Vue.axios.get("/operation/operationPlatform/facelib/getProvince").then(({data}) =>{
                    let provinces = data.filter((item:any) =>{
                        return item.id !== -1;
                    })
                    callback(provinces);
                });
            }else if(level == 1){
                Vue.axios.get("/operation/operationPlatform/facelib/getCity?provinceId=" + node.data.id).then(({data}) =>{
                    let cities = data.filter((item:any) =>{
                        return item.id != -1;
                    })
                    callback(cities);
                });
            }else if(level == 2){
                Vue.axios.get("/operation/operationPlatform/facelib/getArea?cityId=" + node.data.id).then(({data}) =>{
                    let countries:any[] = [];
                    data.forEach((item:any) =>{
                        if(item.id != -1){
                            item.leaf = true;
                            countries.push(item);
                        }
                    });
                    callback(countries);
                });
            }
        }
    };
    created(){
        this.axios.get("/json/cascader.json").then(({data}) =>{
            this.jsonData = data;
        })
    }
    handleChange(val:any){
        console.log(val);
    }
}
</script>

<style scoped>

</style>
